<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\Custom;
use app\appv110\model\StaffLink;
use app\util\Callout;
use app\appv110\model\Pic;
use app\appv110\model\Opinion;
use app\appv110\model\AppAudit;
use app\appv110\model\Staff;

class PublicController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 10000  吐槽失败
         * 10001  
         * 10002  
         */
    }

    /*
     * 虚拟电话绑定
     * author xfwed
     * date 2019.11.07
     */

    public function midNumBind() {//$midNum,$phone,$expiration
        $phone = input('param.phone');
        $staffId = input('param.staff_id');
        //return Utils::ajaxReturn(0, '绑定成功', $phone);
        $caller = Staff::where('id',$staffId)->value('telephone');
       $midNum = '16210500865';//17081082707  15669475259
        $expiration = 180;
        $Callout = new Callout();
        //$result = $Callout->midNum($midNum, $phone, $expiration);
        $result = $Callout->midNumAXB($midNum, $phone, $caller, $expiration);

        $result = json_decode($result, true);
        $status = $result['success'];
        //根据用户手机号查询用户id
        $ctmId = Custom::where('custom_phone', $phone)->value('id');
        $data = [
            'staff_id' => $staffId,
            'ctm_id' => $ctmId,
            'add_time' => date('Y-m-d H:i:s', time()),
        ];
        StaffLink::insert($data);
        if ($status) {
            return Utils::ajaxReturn(0, '绑定成功', $midNum);
        } else {
            $message = $result['message'];
            //return Utils::ajaxReturn(1, $message, $result);
            return Utils::ajaxReturn(0, '绑定成功', $phone);
        }
    }

    /*
     * 启动图片
     * 作者 xfwed
     * 日期 2019-11-14
     */

    public function startPic() {
        $version = input('param.version');
        $row = Pic::where('type', '=', 5)
                ->where('pro_type', '=', 2)
                ->where('isshow', 1)
                ->field('id, pic,pic2, url') 
                ->order('id asc')
                ->find();
        $iosType = input('param.iosType');//1是2否 需要其他尺寸
        if($iosType == 1){
           $row['pic'] = $row['pic2']; 
        }
        if($version){
            $appAudit = AppAudit::where('version',$version)->where('is_open',1)->find();
            if($appAudit){
                $row['requestUrl'] = AppAudit::field('c_java pjava,c_h5 h5,c_php php,c_apk apk,c_phpxcx phpxcx,c_javaxcx javaxcx')->find();
            }  else {
                $row['requestUrl'] = AppAudit::field('z_java pjava,z_h5 h5,z_php php,z_apk apk,z_phpxcx phpxcx,z_javaxcx javaxcx')->find();
            }
        }
        return Utils::ajaxReturn(0, '查询成功', $row);
    }
    
    /*
     * 用户吐槽
     * 作者 xfwed
     * 日期 2020-06-29
     */
    public function addOpinion(){
        $rule = [
            'type' => 'require',
            'userid' => 'require',
            'content' => 'require',

        ];
        $msg = [
            'type.require' => '类型必须',
            'userid.require' => '用户id必须',
            'content.require' => '内容必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $type = $all['type'];
        $userid = $all['userid'];
        $content = $all['content'];
        $data['type'] = $type;
        $data['userid'] = $userid;
        $data['content'] = $content;
        $data['add_time'] = date('Y-m-d H:i:s');
        
        $row = Opinion::insert($data);
        if($row){
            return Utils::ajaxReturn(0, '吐槽成功', null);
        }  else {
            return Utils::ajaxReturn(10000, '吐槽失败', null);
        }
    }

}
